/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import core.StepOwnerCore;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Dapversion;
import model.Decision;
import model.Stepvalidation;

/**
 *
 * @author phinguyen
 */
public class Servletchoixstep extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        RequestDispatcher rd = null;
        try {
//            StepOwnerCore core = new StepOwnerCore();
//            System.out.println("recuperation decision : " + request.getParameter("decisionid_hidden"));
//            Decision decision = core.getDecisionById(new Integer(request.getParameter("decisionid_hidden")));
//            Dapversion dapversion = decision.getDapversion();
//            //System.out.println("decision : " + decision);
            System.out.println("valide : " + request.getParameter("return_txt"));
            StepOwnerCore core = new StepOwnerCore();
            Stepvalidation nextStep;
            nextStep = core.getStepbyid(new Integer(request.getParameter("return_txt")));
            
            Decision newDecision = new Decision((Dapversion) request.getSession().getAttribute("dapversion"), nextStep, "In progress");
            System.out.println("après validation : " + newDecision.toString());
            core.saveNewDecision(newDecision);
            rd = request.getRequestDispatcher("ServletSetHomeEmployee");
            

            //if on invalide
            //on renvoi vers une page pour le choix du step à invalider
            //sinon si le step que l'on valide est le dernier du dap
            //on valide mais on ne créé pas de decision suivante
            //sinon
            //on valide et on créé la décision suivante
//            boolean verif = core.verifstepfinal(decision.getStepvalidation().getSvid(), dapversion.getDvid());
//            if (verif == true) {
//                System.out.println("passe dans le if 1 : c'est la décision finale d'un dapStructure");
//                rd = request.getRequestDispatcher("ServletSetHomeEmployee");
//                decision.setDstate(request.getParameter("valid"));
//                core.updateDecision(decision);
//            } else {
//                System.out.println("passe dans le else 1 : ce n'est pas la décision finale d'un dapStructure");
//                if ("Validate".equals(request.getParameter("valid"))) {
//                    System.out.println("passe dans le if 2 : c'est une validation");
//                    System.out.println("dapversion : " + dapversion.getDvidvtitle() + " core.getStepbyid(decision.getDid() + 1) : " + core.getStepbyid(decision.getStepvalidation().getSvid() + 1).getSvid() + ", \"In progress\");");
//
//                    System.out.println("save effectué");
//                    rd = request.getRequestDispatcher("index.jsp");
//                    decision.setDstate(request.getParameter("valid"));
//                    core.updateDecision(decision);
//                    System.out.println("etta" + request.getParameter("valid"));
//                } else {
//                    System.out.println("passe dans le else 2 : c'est une invalidation");
//                    request.getSession().setAttribute("lstreturn", core.getNextDapComposition(decision));
//                    System.out.println("après invalidation : " + core.getNextDapComposition(decision).toString());
//                    rd = request.getRequestDispatcher("choiceNextStep.jsp");
//                    decision.setDstate(request.getParameter("valid"));
//                    core.updateDecision(decision);
//                }
//
//            }
            rd.forward(request, response);
        } finally {

        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
